package apptoko.com.view;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import apptoko.com.util.CommonUtils;
import apptoko.com.util.Utils;
import apptoko.com.validator.Form;
import apptoko.com.validator.Validate;
import apptoko.com.validator.validator.NotEmptyValidator;

import apptoko.com.R;
;

public class FeedBackDialog extends Dialog {
	private TextView tvTitle;

	private TextView tvContent;

	private Context mContext;

	private Form form;
	private Spinner spnErrorType;
	private EditText editFeedback;
	private Button btnConfirm;
	private Button btnCancel;
	private String content="";
	private String username="";


	public FeedBackDialog(Context context,int type,String content,String username) {
		super(context);
		init(context,type, content, username);
	}

	private void init(Context context,int type,final String content,final String username) {
		this.content = content;
		this.username = username;
		this.mContext = context;
		this.getWindow().getAttributes().windowAnimations = android.R.style.Animation_Dialog;
		this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_feedback);
		tvTitle = (TextView) findViewById(R.id.tvTitle);
		spnErrorType = (Spinner) findViewById(R.id.spn_feedback);
		if(type==0){
			spnErrorType.setAdapter(new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,mContext.getResources().getStringArray(R.array.feed_back_common)));
		}else{
			spnErrorType.setAdapter(new ArrayAdapter<String>(mContext,android.R.layout.simple_list_item_1,mContext.getResources().getStringArray(R.array.feed_back_detail)));
		}
		editFeedback = (EditText) findViewById(R.id.edit_feedback);
		btnConfirm = (Button) findViewById(R.id.btn_confirm);
		btnCancel = (Button) findViewById(R.id.btn_cancel);
		form = new Form();
		Validate serialValid = new Validate(editFeedback);
		serialValid.addValidator(new NotEmptyValidator(mContext));
		form.addValidates(serialValid);
		btnConfirm.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if(form.validate()){
					String message="";
					String selected = (String) spnErrorType.getSelectedItem();
						message +=mContext.getResources().getString(R.string.top_email_content)+" "+editFeedback.getText().toString()+"\n\n\n\n\n\n\n---------------\n"+mContext.getResources().getString(R.string.error_type)+ " "+selected+"\n";
					
					SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
					Date now = new Date();
					String date = simpleDateFormat.format(now);
					message += mContext.getResources().getString(R.string.content)+" "+content+"\n";
					message += mContext.getResources().getString(R.string.usernamefeedback)+" "+username+"\n";
					message += mContext.getResources().getString(R.string.devicename)+" "+CommonUtils.getDeviceName()+"\n";
					message += mContext.getResources().getString(R.string.android_version)+" "+CommonUtils.getDeviceVersion()+"\n";
					message += mContext.getResources().getString(R.string.version_app)+" "+Utils.getVersion(mContext)+" build: "+Utils.getVersionCode(mContext)+"\n";
					message += mContext.getResources().getString(R.string.timetosend)+" "+date+"\n";
					Intent email = new Intent(Intent.ACTION_SEND);
					email.putExtra(Intent.EXTRA_EMAIL, new String[]{"support@apptoko.com"});		  
					email.putExtra(Intent.EXTRA_SUBJECT, "[Feedback] From: "+username+" [Apptoko v"+Utils.getVersion(mContext)+" build: "+Utils.getVersionCode(mContext)+"]");
					email.putExtra(Intent.EXTRA_TEXT, message);
					email.setType("message/rfc822");
					mContext.startActivity(Intent.createChooser(email, "Choose an Email client :"));
					dismiss();
				}
			}
		});
		btnCancel.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});
	}

	public void setTitle(int resId) {
		if (resId != 0)
			tvTitle.setText(mContext.getResources().getString(resId));
		else
			tvTitle.setVisibility(View.GONE);
	}

	public void setTitle(String title) {
		tvTitle.setText(title);
	}

	public void setContent(int resId) {
		tvContent.setText(mContext.getResources().getString(resId));
	}


}
